package com.utils.httpclient;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * HTTP 参数构建
 * 新增mapToXml
 * @author chihaojie
 *
 */
public class HttpParamsBuilder {

	/**
	 * GET请求参数构建
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	public static String getParamsBuilder(String url,
			Map<String, String> params, String encode) {
		if (null != params && !params.isEmpty()) {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			if (!url.contains("?") || !url.endsWith("?")) {
				url += "?";
			}
			Iterator<String> it = params.keySet().iterator();
			String key = null;
			while (it.hasNext()) {
				key = it.next();
				nvps.add(new BasicNameValuePair(String.valueOf(key), String.valueOf(params.get(key))));
			}
			url += URLEncodedUtils.format(nvps, encode);
		}
		return url;
	}

	/**
	 * POST请求参数构造
	 * 
	 * @param params
	 * @return
	 */
	public static List<NameValuePair> formParamsBuilder(
			Map<String, String> params) {
		if (null != params && !params.isEmpty()) {
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			Iterator<String> it = params.keySet().iterator();
			String key = null;
			while (it.hasNext()) {
				key = it.next();
				nvps.add(new BasicNameValuePair(key, params.get(key)));
			}
			return nvps;
		}
		return null;
	}

	/**
	 * POST请求参数构造
	 * 
	 * @param params
	 * @return
	 */
	public static String jsonParamsBuilder(Map<String, String> params) {
		if (null != params && !params.isEmpty()) {
			return JSONObject.toJSONString(params);
		}
		return null;
	}
	/**
	 * XML请求参数构建
	 */

	public  static String  mapToXmlBuilder(Map<String, String> data) throws Exception{
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document document = documentBuilder.newDocument();
		Element root = document.createElement("xml");
		document.appendChild(root);
		Iterator var5 = data.keySet().iterator();

		while(var5.hasNext()) {
			String key = (String)var5.next();
			String value = (String)data.get(key);
			if (value == null) {
				value = "";
			}

			value = value.trim();
			Element filed = document.createElement(key);
			filed.appendChild(document.createTextNode(value));
			root.appendChild(filed);
		}

		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		DOMSource source = new DOMSource(document);
		transformer.setOutputProperty("encoding", "UTF-8");
		transformer.setOutputProperty("indent", "yes");
		StringWriter writer = new StringWriter();
		StreamResult result = new StreamResult(writer);
		transformer.transform(source, result);
		String output = writer.getBuffer().toString();

		try {
			writer.close();
		} catch (Exception var12) {
			;
		}
       return output;
	}

	/**
	 * MAP构建
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> mapParamsBuilder(Object obj) {
		return JSONObject.parseObject(JSONObject.toJSONString(obj), Map.class);
	}
	
	/**
	 * 字符串转OBJECT
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public static <T> T objectParamsBuilder(String params,Class<T> clazz){
		return JSONObject.parseObject(params, clazz);
	}
	
	/**
	 * 字符串转LIST
	 * 
	 * @param params
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> arrayParamsBuilder(String params,Class<T> clazz){
		return JSONObject.parseArray(params, clazz);
	}
}
